<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="photo" label="医生照片">
        <uni-file-picker file-mediatype="image" file-extname="jpg,png" return-type="object" v-model="formData.photo"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="name" label="姓名" required>
        <uni-easyinput placeholder="医生的姓名" v-model="formData.name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="title" label="职称" required>
        <uni-easyinput placeholder="医生的职称" v-model="formData.title" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="department" label="所属科室" required>
        <uni-easyinput placeholder="医生所属的科室" v-model="formData.department" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="specialty" label="主治" required>
        <uni-easyinput placeholder="医生的主治方向" v-model="formData.specialty" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="fee" label="挂号费" required>
        <uni-easyinput placeholder="医生的挂号费用" type="number" v-model="formData.fee"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/doctorSchema.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'doctorSchema';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "photo": null,
        "name": "",
        "title": "",
        "department": "",
        "specialty": "",
        "fee": null
      }
      return {
        formData,
        formOptions: {},
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
